package cn.xfdzx.android.apps.shop.activity;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.AfterSalesListMarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterSalesListRetailAdapter;
import cn.xfdzx.android.apps.shop.adapter.OrderListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListMarketBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.NewOrderBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.OrderListContract;
import cn.xfdzx.android.apps.shop.presenter.OrderListPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/*售后列表页面*/
public class AfterSalesActivity extends BaseMvpActivity<OrderListPresenter> implements OrderListContract.View {

    @BindView(R.id.rv_all_order)
    RecyclerView rvAllOrder;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    //private OrderListAdapter orderListAdapter;
    private AfterSalesListRetailAdapter orderListAdapter;
    private int page = 1;
    private int limit = Constants.LIMIT;
    private List<AfterSalesListRetailBean.DataBean.ApplyBean> applyBeanList = new ArrayList<>();//商品列表
    String sales_model;
    private View emptyView;
    private List<AfterSalesListMarketBean.DataBean> marketAfterListdata;
    private AfterSalesListMarketAdapter salesListMarketAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_after_sales;
    }

    @Override
    public void initView() {
        mPresenter = new OrderListPresenter();
        mPresenter.attachView(this);
        if (TYPE_IS_RETAIL == 0){
            sales_model="0";
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvAllOrder.setLayoutManager(linearLayoutManager);
            rvAllOrder.setItemAnimator(new DefaultItemAnimator());
            orderListAdapter = new AfterSalesListRetailAdapter(applyBeanList);
            rvAllOrder.setAdapter(orderListAdapter);
            emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //marketAdapter.setHeaderAndEmpty(true);
            orderListAdapter.setEmptyView(emptyView);
        }else {
            sales_model="1";
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvAllOrder.setLayoutManager(linearLayoutManager);
            rvAllOrder.setItemAnimator(new DefaultItemAnimator());
            salesListMarketAdapter = new AfterSalesListMarketAdapter(marketAfterListdata);
            rvAllOrder.setAdapter(salesListMarketAdapter);
            emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //marketAdapter.setHeaderAndEmpty(true);
            salesListMarketAdapter.setEmptyView(emptyView);
        }


        if (sales_model.equals("0")){
            getAfterList(true,true);
        }else {
            getAfterMarketList(true,true);
        }


        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (sales_model.equals("0")){
                    getAfterList(true,true);
                }else {
                    getAfterMarketList(true,true);
                }

            }
        });
    }

    /**请求批发售后列表*/
    private void getAfterMarketList(boolean isReference,boolean isLoading) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("salesMode", sales_model);

        mPresenter.getAfterSalesMarketList(isReference, isLoading, map);
    }

    /**请求零售售后列表*/
    private void getAfterList(boolean isReference,boolean isLoading) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("salesMode", sales_model);

        mPresenter.getAfterSalesRetailList(isReference, isLoading, map);
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {
        Log.e("异常", throwable.toString());
    }

    @Override
    public void onOrderListSuccess(OrderListBeen bean) {

    }

    @Override
    public void getRefundRreasonSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void getAfterServiceRequestSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void getReturnReasonSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void onPaySuccess(NewOrderBeen bean, int position) {

    }

    @Override
    public void onConfirmSuccess(ConfirmBeen bean, int position) {

    }

    @Override
    public void onGetAfterSalesRetailListSuccess(AfterSalesListRetailBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        if (sales_model.equals("0")){
                            getAfterList(false,false);
                        }

                    }
                });

                if (EmptyUtil.isNotEmpty(bean.getData().getApply())) {
                    mRefreshLayout.setNoMoreData(false);
                    orderListAdapter.refreshDatas(bean.getData().getApply());
                }
            }else {
                if (EmptyUtil.isNotEmpty(bean.getData().getApply())) {
                    orderListAdapter.addData(bean.getData().getApply());
                }
            }
            if (EmptyUtil.isEmpty(bean.getData().getApply())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onGetIsExceedTimeSuccess(AfterSalesListRetailBean bean) {

    }

    @Override
    public void onGetAfterSalesMarketListSuccess(AfterSalesListMarketBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        if (sales_model.equals("1")){
                            getAfterMarketList(false,false);
                        }

                    }
                });

                if (EmptyUtil.isNotEmpty(bean.getData())) {
                    mRefreshLayout.setNoMoreData(false);
                    marketAfterListdata = bean.getData();
                    salesListMarketAdapter.refreshDatas(marketAfterListdata);
                }
            }else {
                if (EmptyUtil.isNotEmpty(bean.getData())) {
                    salesListMarketAdapter.addData(bean.getData());
                }
            }
            if (EmptyUtil.isEmpty(bean.getData())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
